package com.test.dao.impl;

import com.test.dao.ChoiceDao;
import com.test.domain.Choice;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.CallableStatementCallback;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.Types;
import java.util.List;

@Repository
public class ChoiceDaoImpl implements ChoiceDao {

    @Resource
    private JdbcTemplate jdbcTemplate;

    @Override
    public Integer getCountByCustomerPhoneNumberAndDishNumber(String customerPhoneNumber, String dishNumber) {
        String isExistSql = "select count(*) from choice where customer_phone_number=? and dish_number=?";
        return jdbcTemplate.queryForObject(isExistSql, Integer.class, customerPhoneNumber,dishNumber);
    }

    @Override
    public void insertChoice(String customerPhoneNumber, String dishNumber) {
        String sql= "insert into choice values(?,?,1)";
        jdbcTemplate.update(sql,customerPhoneNumber,dishNumber);
    }

    @Override
    public void updateChoiceDishNum(String customerPhoneNumber, String dishNumber) {
        String sql= "update choice set dish_num=dish_num+1 where customer_phone_number=? and dish_number=?";
        jdbcTemplate.update(sql,customerPhoneNumber,dishNumber);
    }

    @Override
    public void updateChoiceDishNumS(String customerPhoneNumber, String dishNumber) {
        String sql= "update choice set dish_num=dish_num-1 where customer_phone_number=? and dish_number=?";
        jdbcTemplate.update(sql,customerPhoneNumber,dishNumber);
    }

    @Override
    public void deleteChoiceByDishNumberAndCustomerPhoneNumber(String dishNumber, String customerPhoneNumber) {
        String sql="delete choice where dish_number=? and customer_phone_number=?";
        jdbcTemplate.update(sql,dishNumber,customerPhoneNumber);
    }

    @Override
    public void createOrder(String accountNumber, String allAddress, String dishNumber, String orderNumber) {
        Object execute_test = jdbcTemplate.execute("execute create_order ?,?,?,?,? ",
                (CallableStatementCallback) cs -> {
                    cs.setObject("account_number",accountNumber);
                    cs.setObject("DishNumber",dishNumber);
                    cs.setObject("OrderNumber",orderNumber);
                    cs.setObject("all_address",allAddress);
                    cs.setObject("flag",-1);
                    cs.registerOutParameter("flag", Types.INTEGER);
                    cs.execute();
                    Object ksbm = cs.getObject("flag");
                    return ksbm;
                });
        System.out.println(execute_test);

    }

}
